import { PrismaClient } from '@prisma/client';
import { generateSlug } from '../src/utils/slug';

const prisma = new PrismaClient();

async function initTestProducts() {
  try {
    console.log('开始初始化测试产品数据...');

    const testProducts = [
      {
        title: 'ChatGPT',
        content: 'ChatGPT是由OpenAI开发的大型语言模型，能够进行自然语言对话，回答问题，协助写作等任务。',
        summary: 'OpenAI开发的大型语言模型，支持自然语言对话',
        categoryId: 1,
        status: true,
        coverImage: 'https://images.unsplash.com/photo-1677442136019-21780ecad995?w=400&h=300&fit=crop',
        screenshots: [
          'https://images.unsplash.com/photo-1677442136019-21780ecad995?w=800&h=600&fit=crop',
          'https://images.unsplash.com/photo-1677442136019-21780ecad995?w=800&h=600&fit=crop'
        ],
        isTop: true,
        isRecommend: true,
        websiteUrl: 'https://chat.openai.com',
        developerAvatar: 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=100&h=100&fit=crop&crop=face',
        developerNickname: 'OpenAI团队',
        developerBio: 'OpenAI是一家致力于开发安全人工智能的研究公司，专注于创建有益于全人类的AI技术。'
      },
      {
        title: 'GitHub Copilot',
        content: 'GitHub Copilot是一个AI编程助手，能够帮助开发者编写代码，提供智能代码建议和自动补全功能。',
        summary: 'AI驱动的编程助手，提供智能代码建议',
        categoryId: 1,
        status: true,
        coverImage: 'https://images.unsplash.com/photo-1555066931-4365d14bab8c?w=400&h=300&fit=crop',
        screenshots: [
          'https://images.unsplash.com/photo-1555066931-4365d14bab8c?w=800&h=600&fit=crop',
          'https://images.unsplash.com/photo-1555066931-4365d14bab8c?w=800&h=600&fit=crop'
        ],
        isTop: true,
        isRecommend: true,
        websiteUrl: 'https://github.com/features/copilot',
        developerAvatar: 'https://images.unsplash.com/photo-1507003211169-0a1dd7228f2d?w=100&h=100&fit=crop&crop=face',
        developerNickname: 'GitHub团队',
        developerBio: 'GitHub是全球最大的代码托管平台，致力于为开发者提供最好的协作工具。'
      },
      {
        title: 'Notion AI',
        content: 'Notion AI是集成在Notion中的AI助手，能够帮助用户写作、头脑风暴、总结文档等。',
        summary: '集成在Notion中的AI写作助手',
        categoryId: 2,
        status: true,
        coverImage: 'https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=400&h=300&fit=crop',
        screenshots: [
          'https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=800&h=600&fit=crop',
          'https://images.unsplash.com/photo-1551288049-bebda4e38f71?w=800&h=600&fit=crop'
        ],
        isTop: false,
        isRecommend: true,
        websiteUrl: 'https://notion.so',
        developerAvatar: 'https://images.unsplash.com/photo-1494790108755-2616b612b786?w=100&h=100&fit=crop&crop=face',
        developerNickname: 'Notion团队',
        developerBio: 'Notion是一个一体化的协作平台，帮助团队更好地组织工作、知识和项目。'
      },
      {
        title: 'Midjourney',
        content: 'Midjourney是一个AI图像生成工具，能够根据文本描述生成高质量的图像作品。',
        summary: 'AI驱动的图像生成工具',
        categoryId: 3,
        status: true,
        coverImage: 'https://images.unsplash.com/photo-1686191128892-3b1c0b5b0b5b?w=400&h=300&fit=crop',
        screenshots: [
          'https://images.unsplash.com/photo-1686191128892-3b1c0b5b0b5b?w=800&h=600&fit=crop',
          'https://images.unsplash.com/photo-1686191128892-3b1c0b5b0b5b?w=800&h=600&fit=crop'
        ],
        isTop: true,
        isRecommend: false,
        websiteUrl: 'https://midjourney.com',
        developerAvatar: 'https://images.unsplash.com/photo-1500648767791-00dcc994a43e?w=100&h=100&fit=crop&crop=face',
        developerNickname: 'Midjourney团队',
        developerBio: 'Midjourney专注于AI图像生成技术，为创作者提供强大的视觉创作工具。'
      },
      {
        title: 'Stable Diffusion',
        content: 'Stable Diffusion是一个开源的AI图像生成模型，支持文本到图像和图像到图像的转换。',
        summary: '开源的AI图像生成模型',
        categoryId: 3,
        status: true,
        coverImage: 'https://images.unsplash.com/photo-1686191128892-3b1c0b5b0b5b?w=400&h=300&fit=crop',
        screenshots: [
          'https://images.unsplash.com/photo-1686191128892-3b1c0b5b0b5b?w=800&h=600&fit=crop',
          'https://images.unsplash.com/photo-1686191128892-3b1c0b5b0b5b?w=800&h=600&fit=crop'
        ],
        isTop: false,
        isRecommend: true,
        websiteUrl: 'https://stability.ai',
        developerAvatar: 'https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?w=100&h=100&fit=crop&crop=face',
        developerNickname: 'Stability AI',
        developerBio: 'Stability AI致力于开发开放、安全、可访问的AI技术，推动创意产业的发展。'
      }
    ];

    for (const productData of testProducts) {
      // 生成唯一的slug
      const existingSlugs = await prisma.product.findMany({
        select: { slug: true }
      });
      const existingSlugList = existingSlugs.map(p => p.slug);
      
      let slug = generateSlug(productData.title);
      let counter = 1;
      while (existingSlugList.includes(slug)) {
        slug = `${generateSlug(productData.title)}-${counter}`;
        counter++;
      }

      const product = await prisma.product.create({
        data: {
          title: productData.title,
          content: productData.content,
          summary: productData.summary,
          author: 'Admin', // 添加默认作者
          category: {
            connect: { id: productData.categoryId }
          },
          status: productData.status,
          coverImage: productData.coverImage,
          screenshots: productData.screenshots,
          isTop: productData.isTop,
          isRecommend: productData.isRecommend,
          websiteUrl: productData.websiteUrl,
          developerAvatar: productData.developerAvatar,
          developerNickname: productData.developerNickname,
          developerBio: productData.developerBio,
          slug,
          views: Math.floor(Math.random() * 10000),
          likes: Math.floor(Math.random() * 1000),
          comments: Math.floor(Math.random() * 100),
        }
      });
      
      console.log(`创建产品: ${product.title} (${product.slug})`);
    }

    console.log('测试产品数据初始化完成！');
  } catch (error) {
    console.error('初始化测试产品数据失败:', error);
  } finally {
    await prisma.$disconnect();
  }
}

initTestProducts(); 